package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Customers;
import cn.springboot.erp.mapper.CustomersMapper;
import cn.springboot.erp.vo.SalesOrdersVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.SalesOrders;
import cn.springboot.erp.service.SalesOrdersService;
import cn.springboot.erp.mapper.SalesOrdersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【sales_orders】的数据库操作Service实现
* @createDate 2024-05-12 10:14:04
*/
@Service
public class SalesOrdersServiceImpl extends ServiceImpl<SalesOrdersMapper, SalesOrders>
    implements SalesOrdersService{

    @Resource
    private CustomersMapper customersMapper;
    @Override
    public ResponseResult listPage(QueryParams<SalesOrders> queryParams) {
        // 获取分页参数
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();
        SalesOrders searchParams = queryParams.getSearchParams();
        // 分页
        Page<SalesOrders> salesOrdersPage = new Page<>(page, limit);
        LambdaQueryWrapper<SalesOrders> lambda = new QueryWrapper<SalesOrders>().lambda();
        lambda.eq(StringUtils.isNotBlank(searchParams.getOrderNumber()),SalesOrders::getOrderNumber,searchParams.getOrderNumber())
                .eq(ObjectUtils.isNotNull(searchParams.getCustomerId()),SalesOrders::getCustomerId,searchParams.getCustomerId())
                .orderByDesc(SalesOrders::getCreateTime);
        baseMapper.selectPage(salesOrdersPage,lambda);

        List<SalesOrdersVO> ordersVO = salesOrdersPage.getRecords().stream().map(order -> {
            SalesOrdersVO salesOrdersVO = new SalesOrdersVO();
            BeanUtils.copyProperties(order, salesOrdersVO);
            // 获取客户id,查询客户信息
            Integer customerId = order.getCustomerId();
            Customers customers = customersMapper.selectById(customerId);
            salesOrdersVO.setCustomerName(customers.getName());
            return salesOrdersVO;
        }).collect(Collectors.toList());
        return ResponseResult.Success()
                .data("total",salesOrdersPage.getTotal())
                .data("ordersVO",ordersVO);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 根据id查询订单信息
        SalesOrders salesOrders = baseMapper.selectById(id);
        // 判断状态
        if (salesOrders.getPaymentStatus()==0){
            return ResponseResult.Fail().msg("订单还未付款，暂时不可删除");
        }
        // 删除订单信息
        baseMapper.deleteById(id);
        return ResponseResult.Success().msg("订单删除成功");
    }
}




